import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ConfigService } from '@nestjs/config';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // 构建swagger文档
  const configService = app.get(ConfigService);
  const options = new DocumentBuilder()
    .setTitle(configService.get<string | undefined>('SWAGGER_TITLE') ?? 'API 文档')
    .setDescription(configService.get<string | undefined>('SWAGGER_DESCRIPTION') ?? 'API 文档')
    .setVersion(configService.get<string | undefined>('SWAGGER_VERSION') ?? '1.0')
    .setContact(
      configService.get<string | undefined>('SWAGGER_CONTACT_NAME') ?? '联系人',
      configService.get<string | undefined>('SWAGGER_CONTACT_URL') ?? '联系人网址',
      configService.get<string | undefined>('SWAGGER_CONTACT_EMAIL') ?? '联系人邮箱',
    )
    .build();
  const document = SwaggerModule.createDocument(app, options);
  if (configService.get<boolean>('SWAGGER_ENABLE')) {
    SwaggerModule.setup(configService.get('SWAGGER_PATH') || '/api/docs', app, document);
    console.log('swagger 文档已启用,路径:', configService.get('SWAGGER_PATH') || '/api/docs');
  }
  await app.listen(process.env.PORT ?? 3000);
  console.log('🚀网关启动成功,端口:', process.env.PORT ?? 3000);
}
bootstrap();
